/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package suncertify.shared;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


/**
 *
 * @author jshah
 * UI Class for Configuration of parameters on the Client side, This screen pops up when the client is launched. This UI screen
 * lets the user choose the mode to connect to server (Local or Network), choose a DB File using a FileChooser or manually enter the path
 */
public class DBConfigUI {
    /**
     * Holds an instance of EventHandler class for this screen where all the events are delegated for corresponding action
     */
    ActionListener handler ;
    /**
     * Frame in which all the component of this screen reside
     */
    JFrame clientConfigScreen;
    /**
     * The root panel to hold the components
     */
    JPanel mainPanel;

    /**
     * location the path of the DBFile can be entered here
     */
    JTextField location;
    /**
     * submit button to submit the path if it is entered manually
     */
    JButton submit;
    /**
     * button to select the default location stored in properties file
     */
    JButton defaultlocation;
    /**
     * choosefile  button to invoke a File chooser object to choose the db file
     */
    JButton choosefile;
    /**
     * dbFileChooser    FileChooser object to choose the desired file
     */
    JFileChooser dbFileChooser;

    public DBConfigUI(ActionListener handler) {

        this.handler = handler;
        this.clientConfigScreen = new JFrame("SET DATABASE LOCATION");
        this.clientConfigScreen.setMinimumSize(new Dimension(500,300));
        this.clientConfigScreen.setResizable(false);
        this.clientConfigScreen.setLayout(new BorderLayout());
        this.clientConfigScreen.add(this.getPanel(),BorderLayout.NORTH);
        this.clientConfigScreen.setVisible(true);

    }


    public JButton getChoosefile() {
        return choosefile;
    }

    public JFrame getClientConfigScreen() {
        return clientConfigScreen;
    }

    public JFileChooser getDbFileChooser() {
        return dbFileChooser;
    }


    public JTextField getLocation() {
        return location;
    }

    public JPanel getMainPanel() {
        return mainPanel;
    }

    public JButton getSubmit() {
        return submit;
    }

    public JButton getDefaultlocation() {
        return defaultlocation;
    }
    
    
    private JPanel getPanel(){

        this.mainPanel = new JPanel(new GridBagLayout());
        this.mainPanel.setBorder(BorderFactory.createTitledBorder("DB LOCATION CONFIGURATION"));
        this.mainPanel.setPreferredSize(new Dimension(500,300));


        
        int anchor = GridBagConstraints.NORTHEAST;
        GridBagConstraints c = new GridBagConstraints();

        //add label for location textfield
        JLabel loclabel = new JLabel("DATABASE LOCATION:");
        this.setMyConstraints(c, 0, 1, anchor);
        this.mainPanel.add(loclabel,c);

        //add text field for dbfile
        this.location = new JTextField(15);
        this.setMyConstraints(c, 2, 1, anchor);
        this.mainPanel.add(location,c);

        //add Button
        this.submit = new JButton("SUBMIT");
        this.submit.addActionListener(handler);
        this.setMyConstraints(c, 2,2, anchor);
        this.mainPanel.add(this.submit,c);

        //add "OR" label
        this.setMyConstraints(c, 1, 3, anchor);
        this.mainPanel.add(new JLabel("OR"),c);

        //add file chooser label
        this.setMyConstraints(c, 0, 4, anchor);
        this.mainPanel.add(new JLabel("CHOOSE A DB FILE:"),c);

        //add button to invoke file chooser
        this.choosefile = new JButton("CHOOSE");
        this.choosefile.addActionListener(handler);
        this.setMyConstraints(c, 2, 4, anchor);
        this.mainPanel.add(this.choosefile,c);

        //add "OR" label
        this.setMyConstraints(c, 1, 5, anchor);
        this.mainPanel.add(new JLabel("OR"),c);

        //add label for default db location
        this.setMyConstraints(c, 0, 6, anchor);
        this.mainPanel.add(new JLabel("CHOOSE PREVIOUS DB LOCATION"),c);

        //add default db button
        this.setMyConstraints(c, 2,6, anchor);
        this.defaultlocation = new JButton("SELECT PREVIOUS");
        this.defaultlocation.addActionListener(handler);
        this.mainPanel.add(this.defaultlocation,c);
        return this.mainPanel;
    }


     private void setMyConstraints(GridBagConstraints c, int gridx, int gridy, int anchor) {
        c.gridx  = gridx;
        c.gridy  = gridy;
        c.anchor = anchor;
        c.insets = new Insets(5, 5, 0, 5);
        c.fill   = GridBagConstraints.BOTH;

    }

   
}
